import com.alibaba.fastjson.JSON;
import finalshu.Result;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 聊天框得到ServletContext内消息
 */
@WebServlet("/textVal")
public class TextVal extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name =req.getParameter("name");
        String toName =req.getParameter("toName");
        String nameText =name+toName;
        resp.setContentType("application/json;charset=utf-8");
        ServletContext sc = this.getServletContext();
        Object attribute = sc.getAttribute(nameText);
        if (null != attribute){
            Result res = new Result(200,"success");
            res.setOther(attribute);
            String ast = JSON.toJSONString(res);
            resp.getWriter().write(ast);
            resp.getWriter().flush();
            resp.getWriter().close();
        }
        else {
            Result res = new Result(300,"success");
            String ast = JSON.toJSONString(res);
            resp.getWriter().write(ast);
            resp.getWriter().flush();
            resp.getWriter().close();
        }
    }
}
